var currentCid = 1; // 当前分类 id
var cur_page = 1; // 当前页
var total_page = 1;  // 总页数
var data_querying = true;   // 是否正在向后台获取数据

$(
    get_categroy(),
    get_movies()
)

// 加载分类
function get_categroy() {
    $.get(
        "/categroy",
        function (resp) {
            if (resp.errno == 200) {
                // 显示数据
                $("#classfy").html("")
                for (var i = 0; i < resp.data.data.length; i++) {
                    datas = resp.data.data;
                    d = datas[i];
                    var content = '<div class="mod_sear_list"><h3>' + d.categroy + '：</h3>';
                    content += '<ul class="mod_category_item">';
                    for (var j = 0; j < datas[i].datas.length; j++) {
                        dd = datas[i].datas[j]
                        if (j == 0) {
                            content += '<li class="selected">';
                        } else {
                            content += '<li>';
                        }
                        content += '<a href="http://127.0.0.1:5000/movies?categroy_id=' + dd.numid + '">';
                        content += dd.title;
                        content += '</a>';
                        content += '</li>';
                    }
                    content += '</ul>'
                    content += '</div>'
                    $("#classfy").append(content)
                }
            } else {
                // 请求失败
                alert(resp.errmsg)
            }
        })
}

//加载电影
function get_movies(c,d) {
    $.get(
        "/movies",
        function (resp) {
            if (resp.errno == 200) {
                data = resp.data;
                $("#movie_list").html("")
                for (var i = 0; i < data.movies.length; i++) {
                    movie = data.movies[i];
                    var content = '<li>\n' +
                        '                            <div class="site-piclist_pic">\n' +
                        '                                <a\n' +
                        '                                        data-qidanadd-albumid="366359900"\n' +
                        '                                        data-qidanadd-episode="0" data-qidanadd-channelid="1"\n' +
                        '                                        data-qidanadd-tvid="366359900" data-qidanadd-vip="0"\n' +
                        '                                        data-widget-qidanadd="qidanadd"\n' +
                        '                                        data-searchpingback-elem="link"\n' +
                        '                                        data-searchpingback-param="page=1&target=3c37e2ca486191ef9035785e0d93f83c&ptype=1-1&site=iqiyi&pos=1"\n' +
                        '                                        data-qipuid="366359900" alt="' + movie.moviename + '" title="' + movie.moviename + '"\n' +
                        '                                        href="' + movie.url + '"\n' +
                        '                                        class="site-piclist_pic_link"\n' +
                        '                                        target="_blank">\n' +
                        '                                    <img width="180" height="236" rseat="dsjp7" alt="' + movie.moviename + '"\n' +
                        '                                         title="' + movie.moviename + '"\n' +
                        '                                         src="' + movie.imagepath + '"\n' +
                        '                                    />\n' +
                        '\n' +
                        '\n' +
                        '                                    <p class="viedo_lt " data-search-pay="ico">\n' +
                        '                                        <span class="icon-member-box"><img\n' +
                        '                                                src="//pic0.iqiyipic.com/common/20171106/ac/1b/vip_100000_v_601_0_20.png"></span>\n' +
                        '                                    </p>\n' +
                        '\n' +
                        '                                    <div class="wrapper-listTitle">\n' +
                        '                                        <div class="mod-listTitle">\n' +
                        '                                            <p class="viedo_rb">\n' +
                        '                                        <span class="icon-vInfo">                            ' + movie.time +
                        '            </span>\n' +
                        '                                            </p>\n' +
                        '                                        </div>\n' +
                        '                                    </div>\n' +
                        '                                </a>\n' +
                        '                            </div>\n' +
                        '                            <div class="site-piclist_info">\n' +
                        '                                <div class="mod-listTitle_left">\n' +
                        '                                    <span class="score"><strong class="num">' + movie.score + '</strong></span>\n' +
                        '                                    <p class="site-piclist_info_title  movie-tit ">\n' +
                        '                                        <a data-searchpingback-elem="link"\n' +
                        '                                           data-searchpingback-param="target=3c37e2ca486191ef9035785e0d93f83c&ptype=1-2&page=1&site=iqiyi&pos=1"\n' +
                        '                                           rseat="bigTitle" title="' + movie.moviename + '"\n' +
                        '                                           href="' + movie.url + '" target="_blank">' + movie.moviename + '</a>\n' +
                        '                                    </p>\n' +
                        '                                </div>\n' +
                        '                            </div>\n' +
                        '                        </li>';
                    $("#movie_list").append(content)
                }
                var page_content = '';
                $("#page_content").html("")
                for (var i = 0; i < data.total_page; i++) {
                    position = i + 1;
                    if (position > 10) {
                        break;
                    }
                    if (i == 0) {
                        if (data.current_page == position) {
                            page_content += '<span class="noPage">上一页</span> ';
                        } else {
                            page_content += '<a data-key="up" data-search-page="item" class="a1" title="跳转至' + (data.current_page - 1) + '页" href="/www/2/-------------24-8-1-iqiyi--.html">上一页</a>';
                        }
                    }

                    if (data.current_page == position) {
                        page_content += '<span data-key="' + position + '" class="curPage">' + position + '</span>';
                    } else {
                        page_content += '<a data-key="2" data-search-page="item"\n' +
                            'title="跳转至第' + position +
                            '页"\n' +
                            'href="/www/1/-------------24-2-1-iqiyi--.html">' + position + '</a>';
                    }

                    if (position == 10) {
                        if (data.current_page == data.total_page) {
                            page_content += '<span class="noPage">下一页</span> ';
                        } else {
                            page_content += '<a\n' +
                                '        data-key="down" class="a1" data-search-page="item" title="跳转至' + position + '页" href="/www/1/-------------24-2-1-iqiyi--.html">下一页</a>';
                        }
                    }

                }
                $("#page_content").append(page_content)
            } else {
                alert("电影请求失败")
            }
        }
    )
}
